package com.zycfc.zsf.boot.mq.rabbitmq.destinations;

import com.zycfc.zsf.boot.mq.rabbitmq.header.*;
import java.util.*;
import com.zycfc.zsf.boot.mq.common.*;
import com.zycfc.zsf.boot.mq.rabbitmq.type.*;
import org.springframework.amqp.core.*;
import org.slf4j.*;

public class FanoutDestination implements IDestination
{
    private static final Logger log;
    private static final String DEFAULT_ROUTING_KEY = "";
    protected final String exchangeName;
    protected final String queueName;
    protected FanoutExchange exchange;
    protected Queue queue;
    private boolean saveErrFlag;
    private Map<String, Object> headerMap;
    
    public FanoutDestination(final Destination destination) {
        this(destination, null, null);
    }
    
    public FanoutDestination(final Destination destination, final HeaderMatchType matchType, final List<IHeaderCode> headers) {
        this.headerMap = new HashMap<String, Object>();
        final String exchangeName = destination.getExchangeName();
        final String queueName = destination.getQueueName();
        this.saveErrFlag = destination.getSaveErrFlag();
        if (null == exchangeName) {
            throw new IllegalArgumentException("\u8bf7\u8bbe\u7f6e\u4ea4\u6362\u5668\u540d\u79f0");
        }
        if (null == queueName || queueName.trim().length() == 0) {
            throw new IllegalArgumentException("\u8bf7\u8bbe\u7f6e\u961f\u5217\u540d\u79f0");
        }
        this.exchangeName = exchangeName.trim();
        this.queueName = queueName.trim();
        if (headers != null && headers.size() > 0) {
            this.headerMap.put("x-match", matchType.getValue());
            for (final IHeaderCode header : headers) {
                this.headerMap.put(header.getName(), header.getValue());
            }
        }
    }
    
    public FanoutExchange exchange() {
        if (null == this.exchange) {
            if (this.headerMap.size() > 0) {
                this.exchange = new FanoutExchange(this.exchangeType().name() + "." + this.exchangeName, true, false, (Map)this.headerMap);
            }
            else {
                this.exchange = new FanoutExchange(this.exchangeType().name() + "." + this.exchangeName, true, false);
            }
        }
        return this.exchange;
    }
    
    @Override
    public Queue queue() {
        if (null == this.queue) {
            final String applicationName = MqEnvConstants.getInstance().getApplicationName();
            final Map<String, Object> args = new HashMap<String, Object>();
            args.put("applicationName", applicationName);
            args.put("env", MqEnvConstants.getInstance().getNamespace());
            this.queue = new Queue(this.exchangeType().name() + "." + this.queueName, true, false, false, (Map)args);
        }
        return this.queue;
    }
    
    @Override
    public final Binding binding() {
        return BindingBuilder.bind(this.queue()).to(this.exchange());
    }
    
    @Override
    public final String getRoutingKey() {
        return "";
    }
    
    @Override
    public final ExchangeType exchangeType() {
        return ExchangeType.FANOUT;
    }
    
    @Override
    public boolean getSaveErrFlag() {
        return this.saveErrFlag;
    }
    
    @Override
    public void resetName(final String namespace, final IDestination destination) {
        final String queueName = destination.queue().getName().trim().split("[.]")[1];
        final String applicationName = MqEnvConstants.getInstance().getApplicationName();
        final Map<String, Object> args = new HashMap<String, Object>();
        args.put("applicationName", applicationName);
        args.put("env", MqEnvConstants.getInstance().getNamespace());
        this.queue = new Queue(namespace + "." + destination.queue().getName().trim(), true, false, false, (Map)args);
        final Map<String, Object> arguments = (Map<String, Object>)destination.exchange().getArguments();
        if (arguments != null && arguments.size() > 0) {
            this.exchange = new FanoutExchange(namespace + "." + destination.exchange().getName(), true, false, (Map)arguments);
        }
        else {
            this.exchange = new FanoutExchange(namespace + "." + destination.exchange().getName());
        }
        this.setRoutingKey(namespace + "." + queueName);
    }
    
    @Override
    public void setRoutingKey(final String routingKey) {
    }
    
    public String getExchangeName() {
        return this.exchangeName;
    }
    
    public String getQueueName() {
        return this.queueName;
    }
    
    public FanoutExchange getExchange() {
        return this.exchange;
    }
    
    public Queue getQueue() {
        return this.queue;
    }
    
    public Map<String, Object> getHeaderMap() {
        return this.headerMap;
    }
    
    public void setExchange(final FanoutExchange exchange) {
        this.exchange = exchange;
    }
    
    public void setQueue(final Queue queue) {
        this.queue = queue;
    }
    
    public void setSaveErrFlag(final boolean saveErrFlag) {
        this.saveErrFlag = saveErrFlag;
    }
    
    public void setHeaderMap(final Map<String, Object> headerMap) {
        this.headerMap = headerMap;
    }
    
    @Override
    public boolean equals(final Object o) {
        if (o == this) {
            return true;
        }
        if (!(o instanceof FanoutDestination)) {
            return false;
        }
        final FanoutDestination other = (FanoutDestination)o;
        if (!other.canEqual(this)) {
            return false;
        }
        final Object this$exchangeName = this.getExchangeName();
        final Object other$exchangeName = other.getExchangeName();
        Label_0065: {
            if (this$exchangeName == null) {
                if (other$exchangeName == null) {
                    break Label_0065;
                }
            }
            else if (this$exchangeName.equals(other$exchangeName)) {
                break Label_0065;
            }
            return false;
        }
        final Object this$queueName = this.getQueueName();
        final Object other$queueName = other.getQueueName();
        Label_0102: {
            if (this$queueName == null) {
                if (other$queueName == null) {
                    break Label_0102;
                }
            }
            else if (this$queueName.equals(other$queueName)) {
                break Label_0102;
            }
            return false;
        }
        final Object this$exchange = this.getExchange();
        final Object other$exchange = other.getExchange();
        Label_0139: {
            if (this$exchange == null) {
                if (other$exchange == null) {
                    break Label_0139;
                }
            }
            else if (this$exchange.equals(other$exchange)) {
                break Label_0139;
            }
            return false;
        }
        final Object this$queue = this.getQueue();
        final Object other$queue = other.getQueue();
        Label_0176: {
            if (this$queue == null) {
                if (other$queue == null) {
                    break Label_0176;
                }
            }
            else if (this$queue.equals(other$queue)) {
                break Label_0176;
            }
            return false;
        }
        if (this.getSaveErrFlag() != other.getSaveErrFlag()) {
            return false;
        }
        final Object this$headerMap = this.getHeaderMap();
        final Object other$headerMap = other.getHeaderMap();
        if (this$headerMap == null) {
            if (other$headerMap == null) {
                return true;
            }
        }
        else if (this$headerMap.equals(other$headerMap)) {
            return true;
        }
        return false;
    }
    
    protected boolean canEqual(final Object other) {
        return other instanceof FanoutDestination;
    }
    
    @Override
    public int hashCode() {
        final int PRIME = 59;
        int result = 1;
        final Object $exchangeName = this.getExchangeName();
        result = result * 59 + (($exchangeName == null) ? 43 : $exchangeName.hashCode());
        final Object $queueName = this.getQueueName();
        result = result * 59 + (($queueName == null) ? 43 : $queueName.hashCode());
        final Object $exchange = this.getExchange();
        result = result * 59 + (($exchange == null) ? 43 : $exchange.hashCode());
        final Object $queue = this.getQueue();
        result = result * 59 + (($queue == null) ? 43 : $queue.hashCode());
        result = result * 59 + (this.getSaveErrFlag() ? 79 : 97);
        final Object $headerMap = this.getHeaderMap();
        result = result * 59 + (($headerMap == null) ? 43 : $headerMap.hashCode());
        return result;
    }
    
    @Override
    public String toString() {
        return "FanoutDestination(exchangeName=" + this.getExchangeName() + ", queueName=" + this.getQueueName() + ", exchange=" + this.getExchange() + ", queue=" + this.getQueue() + ", saveErrFlag=" + this.getSaveErrFlag() + ", headerMap=" + this.getHeaderMap() + ")";
    }
    
    static {
        log = LoggerFactory.getLogger((Class)FanoutDestination.class);
    }
}
